//
//  NBChoseWeatherView.m
//  BeautifulNote
//
//  Created by Apple on 2019/7/18.
//  Copyright © 2019 Maker. All rights reserved.
//

#import "NBChoseWeatherView.h"
#import <TYCyclePagerView.h>

@interface BNWeatherCell : UICollectionViewCell

@property (nonatomic, strong) YYAnimatedImageView *selectedIV;

@end


@implementation BNWeatherCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubViews];
    }
    return self;
}

- (void)addSubViews {
    [self addSubview:self.selectedIV];
    [self.selectedIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (YYAnimatedImageView *)selectedIV {
    if (!_selectedIV) {
        _selectedIV = [[YYAnimatedImageView alloc] init];
        _selectedIV.contentMode = UIViewContentModeScaleAspectFill;
        _selectedIV.layer.masksToBounds = YES;
    }
    return _selectedIV;
}

@end

@interface NBChoseWeatherView ()<TYCyclePagerViewDataSource, TYCyclePagerViewDelegate>

@property (nonatomic, strong) NSArray <YYImage *>*images;
@property (nonatomic, strong) UIButton *backgroundButton;
@property (nonatomic, strong) UIButton *certainButton;
@property (nonatomic, assign) NSInteger selectedIndex;
@property (nonatomic, copy) void(^block)(BNWeatherType weather);
@property (nonatomic, strong) UILabel *titleLb;
@property (nonatomic, strong) TYCyclePagerView *pageView;
@property (nonatomic, strong) TYCyclePagerViewLayout *layout;

@end


@implementation NBChoseWeatherView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubViews];
        self.selectedIndex = 0;
    }
    return self;
}


#pragma mark - action
- (void)clickCertainButton {
    if (self.block) {
        NSArray *tempArr = @[@(BNWeatherTypeSunny),@(BNWeatherTypeRain),@(BNWeatherTypeWindy),@(BNWeatherTypeSnow)];
        self.block([tempArr[self.selectedIndex] integerValue]);
    }
    [self removeFromSuperview];
}

- (void)didClickedBackground {
    [self removeFromSuperview];
}

#pragma mark - collectionViewDelegate & collectionViewDataSource
- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
    return self.images.count;
}

- (__kindof UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
    BNWeatherCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:@"BNWeatherCell" forIndex:index];
    cell.selectedIV.image = self.images[index];
    return cell;
}

- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    return self.layout;
}

- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    self.selectedIndex = toIndex;
}

#pragma mark- private method
- (void)addSubViews {
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
    effectView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    [self addSubview:effectView];
    
    [self addSubview:self.backgroundButton];
    [self.backgroundButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    [self addSubview:self.pageView];
    [self.pageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.width.mas_equalTo(ScreenWidth);
        make.height.mas_equalTo(ScreenWidth);
    }];
    
    [self addSubview:self.titleLb];
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.bottom.mas_equalTo(self.pageView.mas_top).mas_equalTo(-2);
    }];
    
    [self addSubview:self.certainButton];
    [self.certainButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(self.pageView.mas_bottom).mas_equalTo(20);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(40);
    }];
    
}

#pragma mark - publick method
+ (void)showWeatherViewWithSelectedBlock:(void (^)(BNWeatherType weather))block {
    NBChoseWeatherView *moodView = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
    moodView.block = block;
    [[UIApplication sharedApplication].keyWindow addSubview:moodView];
}

#pragma mark - getter & setter
- (TYCyclePagerViewLayout *)layout {
    if (!_layout) {
        _layout = [[TYCyclePagerViewLayout alloc] init];
        _layout.itemSize = CGSizeMake(ScreenWidth - 40, ScreenWidth - 40);
        _layout.itemSpacing = 10;
        _layout.layoutType = TYCyclePagerTransformLayoutCoverflow;
        _layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    }
    return _layout;
}

- (UILabel *)titleLb {
    if (!_titleLb) {
        _titleLb = [UILabel new];
        _titleLb.font = mediumFont(16);
        _titleLb.textColor = [UIColor blackColor];
        _titleLb.textAlignment = NSTextAlignmentCenter;
        _titleLb.text = @"选择天气";
    }
    return _titleLb;
}

- (UIButton *)backgroundButton {
    if (!_backgroundButton) {
        _backgroundButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backgroundButton addTarget:self action:@selector(didClickedBackground) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backgroundButton;
}

- (TYCyclePagerView *)pageView {
    if (!_pageView) {
        _pageView = [[TYCyclePagerView alloc]init];
        _pageView.layer.borderWidth = 1;
        _pageView.isInfiniteLoop = YES;
        _pageView.dataSource = self;
        _pageView.delegate = self;
        _pageView.layer.borderColor = [UIColor clearColor].CGColor;
        [_pageView registerClass:[BNWeatherCell class] forCellWithReuseIdentifier:@"BNWeatherCell"];
    }
    return _pageView;
}

- (UIButton *)certainButton {
    if (!_certainButton) {
        _certainButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_certainButton setTitle:@"确定" forState:UIControlStateNormal];
        _certainButton.layer.cornerRadius = 20;
        _certainButton.layer.masksToBounds = YES;
        _certainButton.backgroundColor = [UIColor grayColor];
        [_certainButton addTarget:self action:@selector(clickCertainButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _certainButton;
}

- (NSArray<YYImage *> *)images {
    if (!_images) {
        _images = @[[YYImage imageNamed:@"sunny.gif"], [YYImage imageNamed:@"rain.gif"], [YYImage imageNamed:@"windy.gif"], [YYImage imageNamed:@"snow.gif"]];
    }
    return _images;
}
@end
